<?php if (!defined('THINK_PATH')) exit();?><!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=0, minimum-scale=1.0, maximum-scale=1.0">
    <title>分享云微商城</title>

    <!-- css -->
    <link href="/mall/Public/Resource/css/bootstrap.min.css" rel="stylesheet">
    <link href="/mall/Public/Resource/css/all.css" rel="stylesheet">
    <script type="text/javascript" src="//libs.baidu.com/jquery/1.9.1/jquery.min.js"></script>
    <script type="text/javascript" src="/mall/Public/Resource/js/common.js"></script>
    <script language='javascript' src='/mall/Public/Resource/js/jquery.cookie.js'></script>
    <script language='javascript' src='/mall/Public/Resource/js/wechat-useragent.js'></script>
</head>
<body>
<div class="navbar-default" role="navigation">
    <div class="container-fluid">
        <ul class="nav">
            <li><a href="/mall/Public/index.php">微商场</a></li>
            <li><a href="/mall/Public/index.php/shoppingcart/index">购物车</a></li>
            <li><a href="/mall/Public/index.php/index/about">分享云简介</a></li>
        </ul>
    </div>
</div>
<div class="main">
    <div class="container-fluid">
        <div class="row">

            <div class="col-xs-12 product-list car-list" id="haveaddress"
                 style="display: none">
                <div class="add-address">
                    <i class="glyphicon glyphicon-map-marker"></i>
                    <p id="name_phone"></p>
                    <p id="address"></p>
                    <a href="/mall/Public/index.php/order/address">修改地址</a>
                </div>
            </div>

            <div class="col-xs-12 product-list car-list" id="noaddress"  style="display: none" >
                <div class="add-address">
                    <i class="glyphicon glyphicon-map-marker"></i>
                    <p>您还没有收货地址</p>
                    <a href="/mall/Public/index.php/order/address">添加地址</a>
                </div>
            </div>





            <div class="col-xs-12 product-list order-list" id="orderlist">
                <p style="font-weight:700">订单详情</p>
                <!--<div class="order">-->
                    <!--<div class="order-img">-->
                        <!--<img src="img/lock.jpg" alt="">-->
                    <!--</div>-->
                    <!--<div class="order-name">-->
                        <!--<p>-->
                            <!--<a href="product.html">互联网智能门锁 X600 ( 防盗门 ) <span>￥3999 x 10</span></a>-->
                        <!--</p>-->
                        <!--<p style="color: #a8a8a8">规格：左开 内开</p>-->
                    <!--</div>-->
                <!--</div>-->
                <!--<div class="order">-->
                    <!--<div class="order-img">-->
                        <!--<img src="img/lock.jpg" alt="">-->
                    <!--</div>-->
                    <!--<div class="order-name">-->
                        <!--<p>-->
                            <!--<a href="product.html">互联网智能门锁 X600 ( 防盗门 ) <span>￥399 x 1</span></a>-->
                        <!--</p>-->
                        <!--<p style="color: #a8a8a8">规格：左开 内开</p>-->
                    <!--</div>-->
                <!--</div>-->
            </div>
            <div class="col-xs-12 product-list order-list">
                <label>订单留言</label>
                <span>
                    <input id="orderremark" type="text" maxlength="50" placeholder="最多50个字">
                </span>
            </div>
            <div class="col-xs-12 product-list order-list">
                <label>支付方式</label>
                <span id="span_wxpay"><input  id="input_wxpay" type="radio" name="pay" value="wxpay" > 微信支付</span>
                <span id="span_alipay"><input id="input_alipay" type="radio" name="pay" value="alipay"  > 支付宝</span>
            </div>
        </div>
    </div>
    <div class="footer">
        <ul class="nav nav-thr">
            <li class="my-cart">
                <span>应付总额 : <span style="color:#e84e40;" id="totalprice"></span></span>
            </li>
            <li class="btn-red"><a href="#" onclick="pay()">支付</a></li>
        </ul>
    </div>

    <div id="alipayinfo" style="display:none"></div>


    <script type="text/javascript">

            var orderdata = "";
            var isbuygateway = '0';//订单是否有购买网关 没有则提示


            if(isWeixinBrowser()){
                document.getElementById('span_wxpay').style.display = '';
                document.getElementById('span_alipay').style.display = 'none';
                document.getElementById('input_wxpay').checked = 'checked';
                document.getElementById('input_wxpay').checked = 'checked';
            }else{
                document.getElementById('span_alipay').style.display = '';
                document.getElementById('span_wxpay').style.display = 'none';
                document.getElementById('input_alipay').checked = 'checked';
            }

            var address = $.cookie('lockshop_address');
            if(isOwnEmpty(address) || typeof address == undefined){
                 document.getElementById('noaddress').style.display = '';
                 document.getElementById('haveaddress').style.display = 'none';
            }else{
                document.getElementById('noaddress').style.display = 'none';
                document.getElementById('haveaddress').style.display = '';

                document.getElementById('name_phone').innerHTML = $.cookie('lockshop_username')
                +" "+$.cookie('lockshop_userphone');

                document.getElementById('address').innerHTML = $.cookie('lockshop_address');

            }


            var orderlist = document.getElementById('orderlist');
            var totalprice = 0;
            var totalnum = 0;
            var keys = "";
            var frombuynowkey = $.cookie('buynow');
            var frombuynow = '0';
            if(isOwnEmpty(frombuynowkey) || typeof frombuynowkey == undefined){
                keys = $.cookie('goodkeys').split('@');
                for(var i=0;i<keys.length;i++) {
                    var key = keys[i];
                    var ischeck = $.cookie("select_"+key);
                    if(!isOwnEmpty(ischeck) && typeof(ischeck) != undefined && ischeck == '1'){
                        //                    alert("calculateprice key:"+key);
                        totalprice =totalprice + parseInt($.cookie(key))*getlockprice(key.split('-')[0]);
                        totalnum = totalnum + parseInt($.cookie(key));

                        initOrderlist(key,parseInt($.cookie(key)));

                    }
                }
            }else{
                frombuynow = '1'
                var key = frombuynowkey;
                var num = parseInt($.cookie(key));
//                alert(key);
                totalprice =  parseInt($.cookie(key))*getlockprice(key.replace('buynow-',"").split('-')[0]);
                totalnum = totalnum + num;
                initOrderlist(key.replace('buynow-',""),num);

            }




           var span_totalprice = document.getElementById('totalprice');

           span_totalprice.innerHTML = "￥"+totalprice;



        function initOrderlist(key,locknum) {
            var data = key.split('-');
            var lockid = data[0];
            var lr = data[1];
            var io = data[2];



            var imageurl = "/mall/Public/Resource/img/lock.jpg";
            var lockname = "";
            var lockprice = "";
            var specification = lr+" "+io;
            specification = specification.replace('left','左开').replace('right','右开')
                    .replace('in','内开').replace('out','外开');


            if(lockid == 600){
                imageurl = "/mall/Public/Resource/img/600z.png";
                lockname = "<?php echo ($x600name); ?>";
                lockprice = "<?php echo ($x600saleprice); ?>";
            }else if(lockid == 610){
                imageurl = "/mall/Public/Resource/img/610z.png";
                lockname = "<?php echo ($x610name); ?>";
                lockprice = "<?php echo ($x610saleprice); ?>";
            }else if(lockid == 666){
                imageurl = "/mall/Public/Resource/img/gateway.png";
                lockname = "<?php echo ($x666name); ?>";
                lockprice = "<?php echo ($x666saleprice); ?>";
            }else if(lockid == 777){
                imageurl = "/mall/Public/Resource/img/box.png";
                lockname = "<?php echo ($x777name); ?>";
                lockprice = "<?php echo ($x777saleprice); ?>";
            }

            if(orderdata==""){
                orderdata = key+"-"+locknum+"-"+lockprice;
            }else{
                orderdata = orderdata+"@"+key+"-"+locknum+"-"+lockprice;
            }

            var parentdiv = document.createElement("div");
            parentdiv.setAttribute('class', 'order');
            var carimg = document.createElement("div");
            carimg.setAttribute('class', "order-img");
            parentdiv.appendChild(carimg);

            var image = document.createElement("img");
            image.setAttribute('src', imageurl);
            carimg.appendChild(image);

            var carname = document.createElement("div");
            carname.setAttribute('class', "order-name");
            parentdiv.appendChild(carname);

            var p = document.createElement("p");
            p.innerHTML = "<a >"+lockname+"<span>￥"+lockprice+"x"+locknum+"</span></a>";
            carname.appendChild(p);

            var p1 = document.createElement("p");
            p1.setAttribute('style', "color:#a8a8a8");
            p1.innerHTML = "规格："+specification;
            if(lockid == '666' || lockid == '777'){
                p1.style.visibility = 'hidden';
            }else{
                p1.style.visibility = 'visible';
            }
            carname.appendChild(p1);



            orderlist.appendChild(parentdiv);
        }



        function getlockprice(lockid) {

            var lockprice = "0";//单价
            if(lockid == 600){
                lockprice = "<?php echo ($x600saleprice); ?>";
            }else if(lockid == 610){
                lockprice = "<?php echo ($x610saleprice); ?>";
            }else if(lockid == 666){
                lockprice = "<?php echo ($x666saleprice); ?>";
                isbuygateway = '1';
            }else if(lockid == 777){
                lockprice = "<?php echo ($x777saleprice); ?>";

            }

            return lockprice;
        }





        function pay(){


            if(isOwnEmpty(address) || typeof address == undefined){
                alert("请填写收货地址");
                return ;
            }
            var orderremark = document.getElementById('orderremark');
            var paytype = $('input:radio[name="pay"]:checked').val();
            if(paytype == null){
                alert("请选择支付方式");
                return ;
            }

            if(totalprice == null || totalprice == '' || totalprice == '0'){
                alert("您的订单有问题,请重新选择商品。");
                return ;
            }

            if(isbuygateway == '0'){
                var r=confirm("没有购买网关会造成门锁使用问题，是否继续完成订单支付。");
                if (r==false)
                {
                    return;
                }
            }

            var from = $.cookie('lockfrom');
            if(isEmpty(from)){
                from = "000";
            }
            $.ajax({
                type:'POST',
                url:'/mall/Public/index.php/order/pay',
                dataType:'json',
                data:{
                    'address':$.cookie('lockshop_address'),
                    'username':$.cookie('lockshop_username'),
                    'userphone':$.cookie('lockshop_userphone'),
                    'remark':orderremark.value,
                    'totalprice':totalprice,
                    'paytype':paytype,
                    'from':from,
                    'order':orderdata
                },
                success:function (data) {


                    if('success' == data.status){

                        if ('alipay' == data.paytype) {

                               $("#alipayinfo").html(data.raw);
                                document.forms['alipaysubmit'].submit();

                        }else if('wxpay' == data.paytype){

                            var wxPackage = data.raw;
                            WeixinJSBridge.invoke('getBrandWCPayRequest', wxPackage,
                                    function (res) {
                                        if ('get_brand_wcpay_request:cancel' == res.err_msg) {

                                        } else if ('get_brand_wcpay_request:fail' == res.err_msg) {

                                        } else if ('get_brand_wcpay_request:ok' == res.err_msg) {
                                            var wxpayURL = '/order/jumporder?orderid=' + data.orderid;

                                            window.location = wxpayURL;
                                        } else if ('access_control:not_allow' == res.err_msg) {
                                            alertshow('支付失败, 没有权限进行支付!');
                                        } else {
                                            alertshow('获取支付状态失败, 我们正在处理, 请以支付结果为准!');
//                                            $.post('/faq/errreport', {type: 'error:js-wxpay', msg: res.err_msg});
                                        }
                                    }
                            );

                        }




                    }else {
                        alert("错误:"+data.msg);
                    }

                },
                error:function (msg) {
//                    alert("error:"+JSON.stringify(msg,undefined, 2));
                }


            });

        }



    </script>

</div>

</body>
</html>